<template>
  <div class="right-top-container">
    <!-- 标题区域 -->
    <div class="title-left">隐患点信息</div>
    
    <div v-if="pageflag" class="hidden-danger-info">
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr1_1.png')" class="info-icon" alt="机构ID">
          <span class="label">机构ID</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.deptId || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr2_1.png')" class="info-icon" alt="边坡名称">
          <span class="label">边坡名称</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.sideName || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr1_2.png')" class="info-icon" alt="边坡类型">
          <span class="label">边坡类型</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.sideType || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr2_2.png')" class="info-icon" alt="边坡等级">
          <span class="label">边坡等级</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.sideLevel || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr1_3.png')" class="info-icon" alt="危险面积">
          <span class="label">危险面积</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.dangerArea || 0 }}㎡</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr2_3.png')" class="info-icon" alt="当前高度">
          <span class="label">当前高度</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.currentHeight || 0 }}m</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr1_4.png')" class="info-icon" alt="边坡趋势">
          <span class="label">边坡趋势</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.trend || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr2_4.png')" class="info-icon" alt="边坡现状">
          <span class="label">边坡现状</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.currentStatus || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr1_5.png')" class="info-icon" alt="雷达ID">
          <span class="label">雷达ID</span>
        </div>
        <div class="info-value">{{ hiddenDangerInfo.radarId || '未知' }}</div>
      </div>
      <div class="info-item">
        <div class="info-header">
          <img :src="require('@/assets/xing-bian-screen/img/tr2_5.png')" class="info-icon" alt="更新时间">
          <span class="label">更新时间</span>
        </div>
        <div class="info-value">{{ formatDate(hiddenDangerInfo.updatedAt) || '未知' }}</div>
      </div>
    </div>
    <Reacquire v-else line-height="200px" @onclick="getData">
      重新获取
    </Reacquire>
  </div>
</template>

<script>
import { getSideInfo } from '@/api/xing-bian-screen/deformation-api'
import tokenManager from '@/utils/tokenManager'

export default {
    props: {
        radarId: {
            type: Number,
            default: 6
        }
    },
    data() {
        return {
            hiddenDangerInfo: {},
            pageflag: false,
            timer: null
        };
    },
    created() {
        this.getData();
        this.timer = setInterval(() => {
            this.getData();
        }, 120000); // 每2分钟刷新一次数据
    },
    watch: {
        radarId: {
            handler(newRadarId) {
                if (newRadarId) {
                    this.getData();
                }
            },
            immediate: false
        }
    },
    beforeDestroy() {
        if (this.timer) {
            clearInterval(this.timer);
        }
    },
    methods: {
        async getData() {
            try {
                // 检查token有效性
                if (!tokenManager.isTokenValid()) {
                    console.warn('Token无效，跳过数据请求');
                    return;
                }
                
                // 获取隐患点信息 - 使用新的API接口参数
                const response = await getSideInfo({
                    deptId: 1,    // 机构ID，必填
                    radarId: this.radarId,  // 雷达ID，必填
                });
                
                if (response && response.data) {
                    this.hiddenDangerInfo = this.processHiddenDangerInfo(response.data);
                    this.pageflag = true;
                } else {
                    this.pageflag = false;
                    // 如果API失败，使用默认数据
                    this.setDefaultData();
                }
            } catch (error) {
                console.error('获取隐患点信息失败:', error);
                this.pageflag = false;
                // 如果API失败，使用默认数据
                this.setDefaultData();
            }
        },
        
        processHiddenDangerInfo(data) {
            // 直接返回API数据，不进行额外处理
            return data;
        },
        
        setDefaultData() {
            // 仅在接口失败时使用的默认数据，显示空值或未知状态
            this.hiddenDangerInfo = {
                deptId: '未知',
                sideName: '未知',
                sideType: '未知',
                sideLevel: '未知',
                dangerArea: 0,
                currentHeight: 0,
                trend: '未知',
                currentStatus: '数据获取失败',
                threatObjects: '未知',
                radarId: '未知',
                createdAt: null,
                updatedAt: null
            };
            this.pageflag = true;
        },
        
        // 格式化日期显示
        formatDate(dateString) {
            if (!dateString) return '未知';
            try {
                const date = new Date(dateString);
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hour = String(date.getHours()).padStart(2, '0');
                const minute = String(date.getMinutes()).padStart(2, '0');
                return `${year}-${month}-${day} ${hour}:${minute}`;
            } catch (error) {
                return '格式错误';
            }
        }
    }
};
</script>

<style lang="scss" scoped>
.right-top-container {
  width: 100%;
  height: 100%;
  /* padding: 10px; */
  box-sizing: border-box;
  
  .title-left {
    color: #ffffff;
    font-size: 18px;
    font-weight: bold;
    background-image: url("../../../assets/xing-bian-screen/img/biaoti.png");
    background-size: contain;
    background-repeat: no-repeat;
    background-position: left center;
    padding-left: 46px;
    height: 50px;
    line-height: 50px;
    width: 100%;
    margin: 0;
  }
  
  .hidden-danger-info {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 20px;
    height: calc(100% - 60px);
    margin-top: 10px;
    
    .info-item {
      display: flex;
      flex-direction: column;
      margin-bottom: 4px;
      
      .info-header {
        display: flex;
        align-items: center;
        margin-bottom: 4px;
        
        .info-icon {
          width: 16px;
          height: 16px;
          margin-right: 6px;
          flex-shrink: 0;
        }
        
        .label {
          color: #2ECAFF;
          font-size: 16px;
          font-weight: bold;
          flex: 1;
        }
      }
      
      .info-value {
        color: #ffffff;
        font-size: 16px;
        padding-left: 22px;
        line-height: 1.4;
      }
    }
  }
}
</style>